package com.ruiboyun.liveapp.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.ruiboyun.liveapp.R;
import com.ruiboyun.liveapp.base.BaseAdapter;
import com.ruiboyun.liveapp.base.ViewHolder;
import com.ruiboyun.liveapp.bean.Channel;
import com.ruiboyun.liveapp.util.tool.DateUtils;
import com.ruiboyun.liveapp.util.tool.ImageUtils;

import java.util.List;

/**
 * Created by wushiqian on 2017/9/19.
 */

public class ChannelAdapter extends BaseAdapter<Channel> {

    public ChannelAdapter(Context context, List<Channel> list) {
        super(context, list);
    }

    private OnLiveListener onLiveListener;


    public void setOnLiveListener(OnLiveListener onLiveListener) {
        this.onLiveListener = onLiveListener;
    }

    public interface OnLiveListener {
        void onLive(Channel channel);

        void onReview(Channel channel);

        void onShare(View view, Channel channel);
    }

    @Override
    public View getLayoutView(ViewGroup parent, int viewType) {
        return getView(parent, R.layout.recyclerview_item_channel);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final Channel channel = list.get(position);
        holder.getTextView(R.id.title).setText(channel.getTitle());
        holder.getTextView(R.id.start_time).setText("\t"+ DateUtils.long2StrDate(channel.getStart_time()));
        if (channel.getEnd_time() == 0) {
            holder.getTextView(R.id.end_time).setText("\t不限制");

        } else {
            holder.getTextView(R.id.end_time).setText( DateUtils.long2StrDate(channel.getEnd_time()));
        }


        ImageUtils.load(context, channel.getCover_addr(), holder.getImageView(R.id.img));
        holder.getTextView(R.id.live).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onLiveListener != null) {
                    onLiveListener.onLive(channel);
                }
            }
        });
        holder.getTextView(R.id.view_num).setText(channel.getView_times() + "次 观看");
        holder.getTextView(R.id.review).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onLiveListener != null) {
                    onLiveListener.onReview(channel);
                }
            }
        });
        holder.getImageView(R.id.share).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onLiveListener != null) {
                    onLiveListener.onShare(v, channel);
                }
            }
        });
    }
}
